package net.xdclass.config;

import lombok.Data;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.retry.MessageRecoverer;
import org.springframework.amqp.rabbit.retry.RepublishMessageRecoverer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@Data
public class RabbitMQErrorConfig {
    /**
     * 异常交换机
     */
    private String shortLinkErrorExchange="short_link.error.exchange";
    /**
     * 异常队列
     */
    private String shortLinkErrorQueue="short_link.error.queue";
    /**
     * 异常routingKey
     */
    private String shortLinkErrorRoutingKey="short_link.add.error.routing.key";

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Bean
    public TopicExchange errorTopicExchange(){
        return new TopicExchange(shortLinkErrorExchange,true,false);
    }

    @Bean
    public Queue errorQueue(){
        return new Queue(shortLinkErrorQueue,true,false,false);
    }

    @Bean
    public Binding bindingErrorQueueAndExchange(){
        return BindingBuilder.bind(errorQueue()).to(errorTopicExchange()).with(shortLinkErrorRoutingKey);
    }

    @Bean
    public MessageRecoverer messageRecoverer(){
        return new RepublishMessageRecoverer(rabbitTemplate, shortLinkErrorExchange, shortLinkErrorRoutingKey);
    }


}
